package com.campus.web.control;

import com.campus.model.MyClass;
import com.campus.service.ClassService;
import com.campus.web.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @author HLQ
 * @date 2024/11/23 1:10
 */
@Controller
@RequestMapping("/classes")
@Slf4j
@Validated
public class ClassController {

    @Autowired
    private ClassService classService;

    // 添加班级
    // POST /classes
    @PostMapping
    @ResponseBody
    public ResponseResult<String> addClass(@RequestBody @Validated MyClass myClass) {
        log.info("添加班级: {}", myClass);
        int result = classService.addClass(myClass);
        if (result > 0) {
            return ResponseResult.ok("班级添加成功");
        } else {
            return ResponseResult.error(HttpStatus.BAD_REQUEST, "班级添加失败");
        }
    }
    @PostMapping("id/update")
    @ResponseBody
    public ResponseResult<String> updateClassById(@RequestParam @NotBlank Integer classId,
                                                  @RequestBody @Validated MyClass myClass) {
        log.info("更新班级: {}，id: {}", myClass, classId);
        myClass.setId(classId);  // 确保班级 ID 已更新
        int result = classService.updateClassById(myClass);
        if (result > 0) {
            return ResponseResult.ok("班级信息更新成功");
        } else {
            return ResponseResult.error(HttpStatus.BAD_REQUEST, "班级信息更新失败");
        }
    }

    // 将学生添加到班级和分组
    @PostMapping("/students/groups")
    @ResponseBody
    public ResponseResult<String> addClassStudent(@RequestParam @NotBlank Integer classId,
                                                  @RequestParam @NotBlank Integer studentId,
                                                  @RequestParam @NotBlank Integer groupId) {
        log.info("将学生添加到班级，classId: {}, studentId: {}, groupId: {}", classId, studentId, groupId);
        int result = classService.addClassStudent(classId, studentId, groupId);
        if (result > 0) {
            return ResponseResult.ok("学生添加到班级成功");
        } else {
            return ResponseResult.error(HttpStatus.BAD_REQUEST, "学生添加到班级失败");
        }
    }

    // 将教师添加到班级
    @PostMapping("/teachers")
    @ResponseBody
    public ResponseResult<String> addClassTeacher(@RequestParam @NotBlank Integer classId,
                                                  @RequestParam @NotBlank Integer teacherId) {
        log.info("将教师添加到班级，classId: {}, teacherId: {}", classId, teacherId);
        int result = classService.addClassTeacher(classId, teacherId);
        if (result > 0) {
            return ResponseResult.ok("教师添加到班级成功");
        } else {
            return ResponseResult.error(HttpStatus.BAD_REQUEST, "教师添加到班级失败");
        }
    }
}
